<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 组织树形结构图 -->
      <el-card class="tree-card">

        <tree-tools :is-root="true" :tree-node="company" />

        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <tree-tools slot-scope="{data}" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>

      <add-dept ref="addDept" :is-show-dialog.sync="isShowDialog" :tree-node="node" :title="title" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import TreeTools from './component/tree-tools.vue'
// 导入查询部门的api
import { getDepartments } from '@/api/departments.js'

import { tranListToTreeData } from '@/utils/index'

// 导入新增部门的对话框
import AddDept from './component/add-dept.vue'
export default {
  name: 'Departments',

  components: {

    TreeTools,
    AddDept
  },

  data() {
    return {
      company: { },
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },

      title: '',

      // 新增弹层显示
      isShowDialog: false,

      // 子组件点击新增部门，当前部门的节点属性
      node: {},

      // loading效果
      loading: false
    }
  },

  created() {
    this.getDepartments()
  },

  mounted() {

  },

  methods: {
    // 获取部门信息
    async getDepartments() {
      this.loading = true
      const result = await getDepartments()

      this.company = { name: result.companyName, manager: '负责人', id: '' }

      this.departs = tranListToTreeData(result.depts, '')

      console.log(result)

      this.loading = false
    },

    // 新增部门
    async addDepts(node) {
      this.title = '新增部门'
      this.isShowDialog = true
      this.node = node
    },

    // 修改部门
    async editDepts(node) {
      this.title = '编辑部门'
      this.isShowDialog = true
      this.node = node

      // 父组件直接调用子组件的方法
      this.$refs.addDept.getDepartDetail(this.node.id)
    }

  }
}
</script>

<style lang="scss" scoped>

.tree-card {
  padding: 30px  140px;
  font-size:14px;
}

</style>
